package com.zhongkun.datahome.service.req.cmd.assets;

import com.zhongkun.datahome.common.utils.bean.BeanUtils;
import com.zhongkun.datahome.service.domain.DataAssets;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.validator.constraints.Length;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * @Author: lifengsheng
 * @Date: 2025/4/18 17:53
 * @Desc
 */
@Data
public class DataAssetsCmd extends DataAssets {
    @ApiModelProperty("可见房号户型ids")
    private List<Long> houseIds;

    // 以后不用这个
    @ApiModelProperty("普通图片/视频的urls")
    private List<String> urls;

    // 用这个
    @ApiModelProperty("普通图片/视频")
    private List<FileEntity> fileEntityList;

    @ApiModelProperty("批量保存开关，0关，1开")
    private Integer batchSaveFlag;

    @Valid
    @ApiModelProperty("批量保存的实体，只针对新建，且batchSaveFlag=1")
    private List<BatchSaveEntity> batchSaveEntities;


    @Data
    public static class BatchSaveEntity {
        @Length(max = 64, message = "资产标题长度不能超过{max}")
        @ApiModelProperty("资产标题")
        private String assetsTitle;

        @ApiModelProperty("资产文件url")
        private String url;

        @ApiModelProperty("普通图片/视频")
        private List<FileEntity> fileEntityList;
    }

    @Data
    public static class FileEntity {
        @ApiModelProperty("文件url")
        private String url;

        @ApiModelProperty("文件备注")
        private String remark;

        public static FileEntity of(String url) {
            FileEntity fileEntity = new FileEntity();
            fileEntity.setUrl(url);
            return fileEntity;
        }
    }

    public static DataAssetsCmd of(DataAssetsCmd cmd, BatchSaveEntity entity) {
        DataAssetsCmd dataAssetsCmd = new DataAssetsCmd();
        BeanUtils.copyBeanProp(dataAssetsCmd, cmd);
        dataAssetsCmd.setHouseIds(cmd.getHouseIds());

        dataAssetsCmd.setAssetsTitle(entity.getAssetsTitle());
        // 老的
        if (StringUtils.isNotBlank(entity.getUrl())) {
            dataAssetsCmd.setUrls(List.of(entity.getUrl()));
        }
        // 新的
        dataAssetsCmd.setFileEntityList(entity.getFileEntityList());
        return dataAssetsCmd;
    }
}
